<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registrazione utente</title>
<link rel="stylesheet" type="text/css" href="resources/css/style.css">
<script type="text/javascript" src="resources/js/jquery-1.7.1.js"></script>
<script type="text/javascript">
function add() {
	$("#usernameError").html("");
	$("#passwordError").html("");
	
	if($("#username").val() == "" || $("#username").val() == "undefined") {
		$("#usernameError").html("Username vuoto");
		$("#username").focus();
		return false;
	}
	if($("#password").val() == "" || $("#password").val() == "undefined") {
		$("#passwordError").html("Password vuota");
		$("#password").focus();
		return false;
	}
	
	$.ajax({
		url : 'checkUsername.htm',
		type : "POST",
		data : ({
			username : $('#username').val()
		}),
		success : function(res) {
			if (res == "yes" && $('#username').val().match(/^\w+$/)) {
				$.ajax({
					url: "addUser.htm",
					type: "post",
					data: ({
						username: $("#username").val(),
						password: $("#password").val()
					}),
					success: function(ris) {
						if(ris == "success")
							redir("loginPage.htm");
						else if(ris == "notAdmin")
							$("#error").html("<h2>Non sei amministratore</h2>");
					}
				});
			}
			else if (res == "no"){
				$("#error").html("<h2>Username gi&agrave; presente!</h2>");
			}
		}
	});
}
</script>
</head>
<body>
	<div class="box">
		<form method="POST" id="registration">
			<table>
				<tr>
					<td>Username:</td>
					<td><input id="username" name="username" type="text" onfocus="$('#error').html('');" onkeydown="$('#error').html('');$('#usernameError').html('');"/></td>
					<td id="usernameError"></td>
				</tr>
				<tr>
					<td>Password:</td>
					<td><input id="password" name="password" type="password" onfocus="$('#error').html('');" onkeydown="$('#error').html('');$('#passwordError').html('');"/></td>
					<td id="passwordError"></td>
				</tr>
				<tr>
					<td><input type="button" value="Register" onclick="add()"></td>
					<td><input type="reset" value="Reset"></td>
				</tr>
			</table>
		</form>
		<div id="error"></div>
	</div>
</body>
</html>